home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / dev / src / td01_src.lha / td_r0.1 / sl3 / source / rawb / rawb.c next >
Encoding:
C/C++ Source or Header  |  1999-05-16  |  11.4 KB  |  476 lines

  1. /*
  2. **      $VER: rawa.c 1.00 (15.05.1999)
  3. **
  4. **      Creation date : 15.05.1999
  5. **
  6. **      Description       :
  7. **         Standart 3d extension module for tdo.library.
  8. **         Loads and saves the mesh as RAW ASCII file.
  9. **
  10. **
  11. **      Written by Stephan Bielmann
  12. **
  13. */
  14.  
  15. /*************************** Includes *******************************/
  16.  
  17. /*
  18. ** Ansi C includes
  19. */
  20. #include <string.h>
  21.  
  22. /*
  23. ** Amiga includes
  24. */
  25. #include <dos/dos.h>
  26. #include <dos/stdio.h>
  27.  
  28. #include <clib/dos_protos.h>
  29. #include <clib/alib_stdio_protos.h>
  30.  
  31. /*
  32. ** Project includes
  33. */
  34. #include "tdo_public.h"
  35. #include "compiler.h"
  36. #include "tdo.h"
  37.  
  38. /**************************** Defines *******************************/
  39.  
  40. #define Ci_BUFFERS 100    // constant size of all used buffers in this mudule
  41.  
  42. /*********************** Type definitions ***************************/
  43.  
  44. typedef struct {
  45.     TTDOFloat x1,y1,z1,x2,y2,z2,x3,y3,z3;
  46. }RAWTriangle;
  47.  
  48. /*************************** Variables ******************************/
  49.  
  50. /********************** Private functions ***************************/
  51.  
  52. /********************** Public functions ****************************/
  53.  
  54. /****** geoa.library/tdo3XSave ******************************************
  55. *   NAME    
  56. *     tdo3XSave -- Saves the mesh as a 3D file.
  57. *
  58. *   SYNOPSIS
  59. *    error = tdo3XSave( meshhandle,filename,screen)
  60. *                       D1         D2        A0
  61. *
  62. *    ULONG tdo3XSave
  63. *         ( ULONG,STRPTR,struct Screen " );
  64. *
  65. *   FUNCTION
  66. *    The mesh will be saved in a 3 dimensional representation in the
  67. *    filename, without existence checks.
  68. *    The screen parameter is optional and only used if non-NULL, to
  69. *    display a window with additional or special parameters needed to
  70. *    save the mesh, or messages.
  71. *   INPUTS
  72. *     meshhandle    - A valid handle of a mesh.
  73. *    filename      - Name of the file to create.
  74. *    screen        - Pointer to the work screen.
  75. *    
  76. *   RESULT
  77. *     error - RCNOERROR      if all went well.
  78. *            RCNOTIMPL      if the function is not implemented.
  79. *            RCNOMEMORY     if there is not enough memory. 
  80. *            RCWRITEDATA    if an error occured while writing data, no more space.
  81. *            RCOVERFLOW     if the mesh is to extensive for this format.
  82. *            RCNOMATERIAL   if there are no materials, but they are expected.
  83. *           RCNOPART       if there are no parts, but they are expected.
  84. *            RCNOPOLYGON    if there are no polygons, but they are expected.
  85. *            RCNOVERTEX     if there are no vertices, but they are expected.
  86. *            IoErr()        if possible you will get this.
  87. *            tdo errors     if a tdo call failed.
  88. *   EXAMPLE
  89. *    error = tdo3XSave(meshhandle,"ram:test",NULL);
  90. *
  91. *   NOTES
  92. *    By setting the meshhandle to 0 the function returns RCNOERROR if
  93. *    it is implemented or RCNOTIMPL if not !
  94. *
  95. *   BUGS
  96. *   SEE ALSO
  97. ******************************************************************************
  98. *
  99. */
  100. ULONG __saveds ASM tdo3XSave(register __d1 ULONG meshhandle,
  101.                             register __d2 STRPTR filename,
  102.                             register __a0 struct Screen *screen) {
  103.  
  104.     BPTR            rawfile=NULL;
  105.     ULONG            mesh,nofv,nofpo,nofpa,i,j,n;
  106.     TTDOVertexf    v1,v2,v3;
  107.     RAWTriangle    tbuffer[Ci_BUFFERS]; // Ci_BUFFERS * sizeof(RAWTriangle)    triangle buffer
  108.     ULONG            bufferstate;
  109.  
  110.     mesh=meshhandle;
  111.  
  112.     // we were asked for implementation
  113.     if(mesh==0) {
  114.         return(RCNOERROR);
  115.     }
  116.  
  117.     // Ensure that no current polygon is set
  118.     meshPartPolygonEnd(mesh);
  119.  
  120.     nofv=meshNofVerticesGet(mesh);
  121.     nofpo=meshNofPolygonsGet(mesh);
  122.     nofpa=meshNofPartsGet(mesh);
  123.  
  124.     // Check if there are some vertices
  125.     if(nofv==0) {
  126.         return(RCNOVERTEX);
  127.     }
  128.  
  129.     // Check if there are some polygons
  130.     if(nofpo==0) {
  131.         return(RCNOPOLYGON);
  132.     }
  133.  
  134.     // Check if there are some parts
  135.     if(nofpa==0) {
  136.         return(RCNOPART);
  137.     }
  138.  
  139.     /* Open the file for output */
  140.     if((rawfile=Open(filename,MODE_NEWFILE))==NULL) return(IoErr());
  141.  
  142.     /* Change the buffer size of the filehandle to 10k */
  143.     if (SetVBuf(rawfile,NULL,BUF_FULL,10000)!=DOSFALSE) {
  144.         Close(rawfile);
  145.         return(RCNOMEMORY);
  146.     }
  147.  
  148.     // Write the header
  149.     if(FWrite(rawfile,"RAWB",4,1)!=1) {
  150.         Close(rawfile);
  151.         return(RCWRITEDATA);
  152.     }
  153.  
  154.     bufferstate=0;
  155.  
  156.     // Get all parts and write down theyr polygons with material
  157.     for(i=1;i<=nofpa;i++) {
  158.         if((nofpo=meshPartNofPolygonsGet(mesh,i))>0) {
  159.             for(j=1;j<=nofpo;j++) {
  160.                 meshPartPolygonCurrentSet(mesh,i,j);
  161.                 // we are saving triangles, so at least 3 vertices must be there
  162.  
  163.                 if((nofv=meshNofVerticesGet(mesh))>2) {
  164.                     meshVertexGetfv(mesh,1,&v1);
  165.  
  166.                     n=2;
  167.                     while(n<nofv) {
  168.  
  169.                         meshVertexGetfv(mesh,n++,&v2);
  170.                         meshVertexGetfv(mesh,n,&v3);
  171.  
  172.                         tbuffer[bufferstate].x1=v1.x;
  173.                         tbuffer[bufferstate].y1=v1.y;
  174.                         tbuffer[bufferstate].z1=v1.z;
  175.  
  176.                         tbuffer[bufferstate].x2=v2.x;
  177.                         tbuffer[bufferstate].y2=v2.y;
  178.                         tbuffer[bufferstate].z2=v2.z;
  179.  
  180.                         tbuffer[bufferstate].x3=v3.x;
  181.                         tbuffer[bufferstate].y3=v3.y;
  182.                         tbuffer[bufferstate].z3=v3.z;
  183.  
  184.                         // check if the buffer is full and write and initialize it
  185.                         if (++bufferstate==Ci_BUFFERS) {
  186.                             if(FWrite(rawfile,&tbuffer,Ci_BUFFERS*sizeof(RAWTriangle),1)!=1) {
  187.                                 Close(rawfile);
  188.                                 return(RCWRITEDATA);
  189.                             }
  190.                             bufferstate=0;
  191.                         }
  192.                     }
  193.                 }
  194.             }
  195.         }
  196.     }
  197.  
  198.     // check if the buffer has still some elements and write them
  199.     if (bufferstate>0) {
  200.         if(FWrite(rawfile,&tbuffer,bufferstate*sizeof(RAWTriangle),1)!=1) {
  201.             Close(rawfile);
  202.             return(RCWRITEDATA);
  203.         }
  204.         bufferstate=0;
  205.     }
  206.  
  207.     // Close the file
  208.     Close(rawfile);
  209.  
  210.     return(RCNOERROR);
  211. }
  212.  
  213. /****** geoa.library/tdo3XLoad ******************************************
  214. *   NAME    
  215. *     tdo3XLoad -- Load a 3D file and creates a mesh.
  216. *
  217. *   SYNOPSIS
  218. *    error = tdo3XLoad( meshhandle,filename,erroffset,screen)
  219. *                       D1         D2       D3         A0
  220. *
  221. *    ULONG tdo3XLoad
  222. *         ( ULONG,STRPTR,ULONG *,struct Screen " );
  223. *
  224. *   FUNCTION
  225. *    A file which contains a 3 dimensional representation
  226. *    will be examined for all known elements and converted into the
  227. *    mesh.
  228. *    The screen parameter is optional and only used if non-NULL, to
  229. *    display a window with additional or special parameters needed to
  230. *    save the mesh or messages.
  231. *    If an error occurs, erroffset contains the file offset in lines for
  232. *    ascii files and bytes for binary formats.
  233. *   INPUTS
  234. *     meshhandle    - A valid handle of a mesh.
  235. *    filename      - Name of the file to load.
  236. *    erroffset     - Offset where a read error occured.
  237. *    screen        - Pointer to the work screen.
  238. *    
  239. *   RESULT
  240. *     error - RCNOERROR       if all went well.
  241. *            RCNOTIMPL       if the function is not implemented.
  242. *            RCNOMEMORY      if there is not enough memory. 
  243. *            RCUNKNOWNFORMAT if the file format is unknown.
  244. *            RCNOFILE        if the file is not found.
  245. *            RCREADDATA      if an error occured while reading data.
  246. *            RCNOMATERIAL    if there are no materials, but they are expected.
  247. *           RCNOPART        if there are no parts, but they are expected.
  248. *            RCNOPOLYGON     if there are no polygons, but they are expected.
  249. *            RCNOVERTEX      if there are no vertices, but they are expected.
  250. *            IoErr()         if possible you will get this.
  251. *            tdo errors      if a tdo call failed.
  252. *   EXAMPLE
  253. *    error = tdo3XLoad(meshhandle,"ram:test",NULL);
  254. *
  255. *   NOTES
  256. *    By setting the meshhandle to 0 the function returns RCNOERROR if
  257. *    it is implemented or RCNOTIMPL if not !
  258. *
  259. *   BUGS
  260. *   SEE ALSO
  261. ******************************************************************************
  262. *
  263. */
  264. ULONG __saveds ASM tdo3XLoad(register __d1 ULONG meshhandle,
  265.                             register __d2 STRPTR filename,
  266.                             register __d3 ULONG *erroffset,
  267.                             register __a0 struct Screen *screen) {
  268.  
  269.     ULONG            mesh;
  270.     BPTR            rawfile=NULL;
  271.     UBYTE            header[5];
  272.     TTDOFloat        af[9];
  273.     TTDOColorub color;
  274.  
  275.  
  276.     mesh=meshhandle;
  277.  
  278.     // we were asked for implementation
  279.     if(mesh==0) {
  280.         return(RCNOERROR);
  281.     }
  282.  
  283.     // we have one white material and one part
  284.     meshMaterialAdd(mesh);
  285.     color.r=255,color.g=255,color.b=255;
  286.     meshMaterialDiffuseColorSetubc(mesh,1,&color);
  287.     meshPartAdd(mesh);
  288.     meshPartMaterialSet(mesh,1,1);
  289.     
  290.     // check if all went well
  291.     if(meshNofMaterialsGet(mesh)!=1 || meshNofPartsGet(mesh)!=1) {
  292.         return(RCNOMEMORY);
  293.     }
  294.  
  295.     /* Open the file for input */
  296.     if((rawfile=Open(filename,MODE_OLDFILE))==NULL) return(RCNOFILE);
  297.  
  298.     // initializing the offset counter
  299.     (*erroffset)=1;
  300.  
  301.     // Read the header
  302.     if(FRead(rawfile,header,4,1)!=1) {
  303.         Close(rawfile);
  304.         return(RCREADDATA);
  305.     }
  306.  
  307.     (*erroffset)+=4;
  308.  
  309.     if(strncmp(header,"RAWB",4)!=0) {
  310.         Close(rawfile);
  311.         return(RCUNKNOWNFORMAT);
  312.     }
  313.  
  314.     // ensure no current polygon
  315.     meshPartPolygonEnd(mesh);
  316.  
  317.     while(FRead(rawfile,af,sizeof(af),1)==1) {
  318.         meshPartTriangleAdd9fa(mesh,1,af);
  319.         (*erroffset)+=sizeof(af);
  320.     }
  321.     
  322.     return(RCNOERROR);
  323. }
  324.  
  325. /****** geoa.library/tdo3XCheckFile ******************************************
  326. *   NAME    
  327. *     tdo3XCheckFile -- Checks if the file is in the format we expected to load.
  328. *
  329. *   SYNOPSIS
  330. *    error = tdo3XLoad( filename )
  331. *                       D1
  332. *
  333. *    ULONG tdo3XCheckFile
  334. *         ( STRPTR );
  335. *
  336. *   FUNCTION
  337. *    The file its header will be examinated to verify if it is the
  338. *    file format we expect to read with the load function.
  339. *   INPUTS
  340. *    filename      - Name of the file to load.
  341. *    
  342. *   RESULT
  343. *     error - RCNOERROR       if all went well.
  344. *            RCUNKNOWNFORMAT if the file format is unknown.
  345. *            RCNOFILE        if the file is not found.
  346. *            RCREADDATA      if an error occured while reading data.
  347. *            IoErr()         if possible you will get this.
  348. *   EXAMPLE
  349. *    error = tdo3XCheckFile("ram:test");
  350. *
  351. *   NOTES
  352. *
  353. *   BUGS
  354. *   SEE ALSO
  355. ******************************************************************************
  356. *
  357. */
  358. ULONG __saveds ASM tdo3XCheckFile(register __d2 STRPTR filename) {
  359.  
  360.     BPTR    rawfile;
  361.     UBYTE    header[5];
  362.  
  363.     /* Open the file for input */
  364.     if((rawfile=Open(filename,MODE_OLDFILE))==NULL) return(RCNOFILE);
  365.  
  366.     // Read the header
  367.     if(FRead(rawfile,header,4,1)!=1) {
  368.         Close(rawfile);
  369.         return(RCREADDATA);
  370.     }
  371.  
  372.     Close(rawfile);
  373.  
  374.     if(strncmp(header,"RAWB",4)!=0) {
  375.         return(RCUNKNOWNFORMAT);
  376.     }
  377.  
  378.     return(RCNOERROR);
  379. }
  380.  
  381. /****** geoa.library/tdo3XExt ******************************************
  382. *   NAME    
  383. *     tdo3XExt -- Returns the default extension of the file format.
  384. *
  385. *   SYNOPSIS
  386. *    ext = tdo3XExt ( )
  387. *
  388. *    SRPTR tdo3XExt
  389. *         ( );
  390. *
  391. *   FUNCTION
  392. *    The default extension of the file format will be returned
  393. *    as READ_ONLY, NULL terminated string which will be only valid
  394. *    as long as the library is opened.
  395. *   INPUTS
  396. *    
  397. *   RESULT
  398. *     ext - String pointer to the extension, or NULL no default.
  399. *   EXAMPLE
  400. *    ext = tdo3XExt();
  401. *
  402. *   NOTES
  403. *
  404. *   BUGS
  405. *   SEE ALSO
  406. ******************************************************************************
  407. *
  408. */
  409. STRPTR __saveds ASM tdo3XExt () {
  410.     static STRPTR ext="raw";
  411.  
  412.     return(ext);
  413. }
  414.  
  415. /****** geoa.library/tdo3XName ******************************************
  416. *   NAME    
  417. *     tdo3XName -- Returns the file format name string.
  418. *
  419. *   SYNOPSIS
  420. *    name = tdo3XName ( )
  421. *
  422. *    SRPTR tdo3XName
  423. *         ( );
  424. *
  425. *   FUNCTION
  426. *    The file format its name will be returned. This string should not 
  427. *    be to large, about 20 characters maximum.
  428. *    The string is READ_ONLY, NULL terminated and only valid as
  429. *    long as the library is opened.
  430. *   INPUTS
  431. *    
  432. *   RESULT
  433. *     name - String pointer to the name, or NULL no one.
  434. *   EXAMPLE
  435. *    name = tdo3XName();
  436. *
  437. *   NOTES
  438. *
  439. *   BUGS
  440. *   SEE ALSO
  441. ******************************************************************************
  442. *
  443. */
  444. STRPTR __saveds ASM tdo3XName () {
  445.     static STRPTR name="TDO RAW binary";
  446.  
  447.     return(name);
  448. }
  449.  
  450. /************************* End of file ******************************/
  451.